home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue90 / misc / tips / op2konq.pl
Encoding:
Perl Script  |  2003-05-01  |  1.6 KB  |  71 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # very quick hack to convert an opera bookmark file into an xbel
  4. # bookmark file (as the one used by konqueror, I don't know what
  5. # xbel is about, I used my konqueror file as an example)
  6.  
  7. use strict;
  8.  
  9. sub write_xbel_hdr {
  10.     print "<!DOCTYPE xbel>\n";
  11.     print "<xbel folded=\"yes\">\n";
  12. }
  13.  
  14. sub write_xbel_tail {
  15.     print "</xbel>\n";
  16. }
  17.  
  18. # parser as a simple state machine: if 0 it tries to read an URL,
  19. # a FOLDER or a FOLDER end (a dash).
  20. # state 1: reading an URL
  21. # state 2: reading a folder
  22. #
  23. # it ignores the order of the bookmarks -- hopefully I can
  24. # convince konqueror to sort them (it's only interested in the
  25. # name and href of the bookmark)
  26. sub parse_input {
  27.     my($state);
  28.     my($title, $url);
  29.     my($indent);
  30.     $state = 0;
  31.     $indent = "";
  32.     while(<STDIN>) {
  33.         s/^\s+//;
  34.         s/\s+$//;
  35.         if ($state == 0) {
  36.             if ($_ eq "#URL") {    $state = 1; next; }
  37.             if ($_ eq "#FOLDER") { $state = 2; next; }
  38.             if ($_ eq "-") {
  39.                 $indent = substr($indent, 0, -2);
  40.                 print "$indent</folder>\n";
  41.                 next;
  42.             } # finish writing the folder
  43.         } elsif ($state == 1) {
  44.             if (/NAME=(.*)$/) {
  45.                 $title = $1;
  46.             } elsif (/URL=(.*)$/) {
  47.                 $url = $1;
  48.             }
  49.             if ($_ eq "") {
  50.                 print "$indent<bookmark icon=\"html\" href=\"$url\">\n";
  51.                 print "$indent<title>$title</title></bookmark>\n";
  52.                 $state = 0;
  53.             }
  54.         } elsif ($state == 2) {
  55.             if (/NAME=(.*)$/) {
  56.                 $title = $1;
  57.             }
  58.             if ($_ eq "") {
  59.                 print "$indent<folder folded=\"yes\">\n";
  60.                 $indent .= "  ";
  61.                 print "$indent<title>$title</title>\n";
  62.                 $state = 0;
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. write_xbel_hdr;
  69. parse_input;
  70. write_xbel_tail;
  71.